import React, { Component } from 'react'
import store from '../store'
import { add, minus, asyncMinus, asyncSetName } from '../store/actionCreator'

export default class Brother1 extends Component {
  state = {
    name: ''
  }

  add1 = () => {
    // 触发加操作的action,action是一个对象，必须要有的属性是type
    // store.dispatch({ type: 'ADD', payload: 3 })
    store.dispatch(add(5))
  }

  minus1 = () => {
    // store.dispatch({ type: 'MINUS', payload: 2 })
    // store.dispatch(minus(4))

    store.dispatch(asyncMinus(4))
  }

  sure = () => {
    store.dispatch(asyncSetName(this.state.name))
  }

  render () {
    return (
      <div>
        Brother1
        <br />
        <br />
        <br />
        <button onClick={this.add1}>+</button>&nbsp;&nbsp;&nbsp;
        <button onClick={this.minus1}>-</button>
        <br />
        <br />
        <br />
        <input
          value={this.state.name}
          onChange={e => this.setState({ name: e.target.value })}
          type='text'
        />
        &nbsp;&nbsp;
        <button onClick={this.sure}>确定</button>
      </div>
    )
  }
}
